// =================================
// 动态显示数码管设计
// =================================
module Seg_Lut(
	input CLK_50M,
	input Rst_n,
	input [7:1] BUT,
	input [7:0] x,
	input [7:0] y,
	input [15:0] cout,
	output	reg	[5:0]	seg_nCS,	// 6个数码管的片选（LOW），LSB对应最左边
	output	reg	[7:0]	seg_leds,	// 共阳，bit0点、1中间、2左上、3左下、4下、5右下、6右上、7上
	output  reg [7:0] 	led			// 8个LED灯
	);

	//wire define
	wire   [3:0]              data0    ;        // x个位数
	wire   [3:0]              data1    ;        // x十位数
	wire   [3:0]              data2    ;        // y个位数
	wire   [3:0]              data3    ;        // y十位数
	wire   [7:0]              data4    ;        // cout个位数
	wire   [7:0]              data5    ;        // cout十位数
	wire   [7:0]              data6    ;
	
	//提取显示数值所对应的十进制数的各个位
	assign  data0 = x[3:0];       // 个位数
	assign  data1 = x[7:4];       // 十位数
	
	assign  data2 = y[3:0];       // 个位数
	assign  data3 = y[7:4];       // 十位数
	
	assign  data4 = cout[7:0];    // 个位数
	assign  data5 = cout[15:8];   // 十位数
	
	reg    [3:0] num_disp;        // 当前数码管显示的数据
	
	reg flag_1, flag_2, flag_3, flag_4, flag_5, flag_6, flag_7; 	//设置标志位
	reg [23:0]	counter;											// 计数器，用于计时200ms
	reg [2:0]count;
	reg [2:0]count_1;


	localparam			TIME2MS_CNT = 50000;
	reg					clk_2ms;					// 使用clk_2m处理
	integer				clk_cnt	;
	
	localparam			TIME200MS_CNT = 1000_0000;	//1000_0000;
	reg					clk_200ms;				 	// 使用clk_200ms处理
	integer				cnt;

	
// =================================
// 产生200ms周期信号
// =================================
	always@(posedge CLK_50M or negedge Rst_n) begin
		if(!Rst_n) begin 
			clk_200ms <= 1'b0;
			cnt <= 0;
		end
		else if(cnt == TIME200MS_CNT-1) begin
			cnt <= 0;
			clk_200ms <= !clk_200ms;
		end
		else cnt <= cnt + 1;
	end

// =================================
// 得到周期为2ms的时钟
// =================================
always @(posedge CLK_50M or negedge Rst_n)
begin
	if(!Rst_n) begin 
		clk_2ms <= 1'b0;
		clk_cnt <= 0;
	end
	else if(clk_cnt == TIME2MS_CNT-1) begin
		clk_cnt <= 0;
		clk_2ms <= !clk_2ms;
	end
	else clk_cnt <= clk_cnt + 1;
end

always @(posedge clk_200ms)	begin
	if(!BUT[1]) 	flag_1 <= ~flag_1;
	if(!BUT[2])		flag_2 <= ~flag_2;
	if(!BUT[3]) 	flag_3 <= ~flag_3;
	if(!BUT[4])		flag_4 <= ~flag_4;
	if(!BUT[5]) 	flag_5 <= ~flag_5;
	if(!BUT[6])		flag_6 <= ~flag_6;
	if(!BUT[7])		flag_7 <= ~flag_7;
end



// ==============================================================
// 流水灯程序
// 通过移位寄存器控制IO口的高低电平，从而改变LED的显示状态,流水灯
// ==============================================================
always @(posedge clk_200ms or negedge Rst_n) begin
    if (!Rst_n)
        led <= 8'hff;
	else begin
		if (flag_1) begin
		case(led)
			8'b1111_1111: led <= 8'b1111_1110;
			8'b1111_1110: led <= 8'b1111_1101;
			8'b1111_1101: led <= 8'b1111_1011;
			8'b1111_1011: led <= 8'b1111_0111;
			8'b1111_0111: led <= 8'b1110_1111;
			8'b1110_1111: led <= 8'b1101_1111;
			8'b1101_1111: led <= 8'b1011_1111;
			8'b1011_1111: led <= 8'b0111_1111;
			default: led <= 8'hff;
		endcase
		end
		if(!flag_1) 	led <= 8'hff;
		if(flag_6) begin
			case(count)
				3'h0: begin
					led <= 8'b0111_1110;
					count <= count + 1'b1;
				end
				3'h1: begin
					led <= 8'b0011_1100;
					count <= count + 1'b1;
				end
				3'h2: begin
					led <= 8'b0001_1000;
					count <= count + 1'b1;
				end
				3'h3: begin
					led <= 8'b0000_0000;
					count <= count + 1'b1;
				end
				3'h4: begin
					led <= 8'b1110_0111;
					count <= count + 1'b1;
				end
				3'h5: begin
					led <= 8'b1100_0011;
					count <= count + 1'b1;
				end
				3'h6: begin
					led <= 8'b1000_0001;
					count <= count + 1'b1;
				end
				3'h7: begin
					led <= 8'b0000_0000;
					count <= count + 1'b1;
				end
				default:	count <= 4'h0;
			endcase
		end
	end
end

// ==============================
// 数码管显示程序
// ==============================
reg [2:0]cnt_seg;
always @(posedge clk_2ms or negedge Rst_n)	begin
	if(!Rst_n) begin
		seg_nCS  <= 6'b1111_11;
		seg_leds <= 8'hFF;
		cnt_seg  <= 0;
	end
	else if((flag_1 || flag_2 || flag_3 || flag_4 || flag_5)) begin
		if(flag_1) begin
			case(cnt_seg)
				3'h0: begin 
							seg_nCS  <= 6'b1111_10;		// 数码管，左1
							seg_leds <= 8'b0000_1101;
							cnt_seg <= cnt_seg + 1'b1;
							end
				3'h1: begin
							seg_nCS  <= 6'b1111_01;		// 数码管，左2
							seg_leds <= 8'b0000_1101;
							cnt_seg <= cnt_seg + 1'b1;
							end
				3'h2: begin
							seg_nCS  <= 6'b1110_11;		// 数码管，左3
							seg_leds <= 8'b1001_1001;
							cnt_seg <= cnt_seg + 1'b1;
							end
				3'h3: begin
							seg_nCS  <= 6'b1101_11;		// 数码管，左4
							seg_leds <= 8'b0000_0011;
							cnt_seg <= cnt_seg + 1'b1;
							end
				3'h4: begin
							seg_nCS  <= 6'b1011_11;		// 数码管，左5
							seg_leds <= 8'b1001_1111;
							cnt_seg <= cnt_seg + 1'b1;
							end
				3'h5: begin
							seg_nCS  <= 6'b0111_11;		// 数码管，左6
							seg_leds <= 8'b1001_1001;
							cnt_seg <= 0;
							end
				default: 	begin
							seg_nCS  <= 6'b1111_11;
							cnt_seg  <= 0;
							end
			endcase	
		end
		if(flag_2) begin
			case(seg_nCS)
				6'b1111_11: begin 
							seg_nCS  <= 6'b1111_10;		// 数码管，左1
							num_disp <= data0;
							end
				6'b1111_10: begin
							seg_nCS  <= 6'b1111_01;		// 数码管，左2
							num_disp <= data1;
							end
				default:	seg_nCS  <= 6'b1111_11;
			endcase
		end
		if(flag_3) begin
			case(seg_nCS)
				6'b1111_11: begin 
							seg_nCS  <= 6'b1111_10;		// 数码管，左1
							num_disp <= data0;
							end
				6'b1111_10: begin
							seg_nCS  <= 6'b1111_01;		// 数码管，左2
							num_disp <= data3;
							end
				6'b1111_01: begin
							seg_nCS  <= 6'b1110_11;		// 数码管，左3
							num_disp <= data2;
							end
				6'b1110_11: begin
							seg_nCS  <= 6'b1101_11;		// 数码管，左4
							num_disp <= data1;
							end
				default:	seg_nCS  <= 6'b1111_11;
			endcase
		end
		if(flag_4) begin
			case(seg_nCS)
				6'b1111_11: begin
							seg_nCS  <= 6'b1110_11;		// 数码管，左3
							num_disp <= data5[3:0];
							end
				6'b1110_11: begin
							seg_nCS  <= 6'b1101_11;		// 数码管，左4
							num_disp <= data4[7:4];
							end
				6'b1101_11: begin
							seg_nCS  <= 6'b1011_11;		// 数码管，左5
							num_disp <= data4[3:0];
							end
				6'b1011_11: begin
							seg_nCS  <= 6'b0111_11;		// 数码管，左6
							num_disp <= data5[7:4];
							end
				default:	seg_nCS  <= 6'b1111_11;
			endcase
		end
		if(flag_5) begin
			case(seg_nCS)
				6'b1111_11: begin
							seg_nCS  <= 6'b1111_10;
							num_disp <= data0;
							end
				6'b1111_10: begin
							seg_nCS  <= 6'b1111_01;
							num_disp <= data3;
							end			
				6'b1111_01: begin
							seg_nCS  <= 6'b1110_11;
							num_disp <= data2;
							end			
				6'b1110_11: begin
							seg_nCS  <= 6'b1101_11;
							num_disp <= data4[7:4];
							end		
				6'b1101_11: begin
							seg_nCS  <= 6'b1011_11;
							num_disp <= data4[3:0];
							end			
				6'b1011_11: begin
							seg_nCS  <= 6'b0111_11;
							num_disp <= data1;
							end		
				default:	seg_nCS  <= 6'b1111_11;
			endcase
		end
		if((flag_2 || flag_3 || flag_4 || flag_5)) begin
			case(num_disp)
				4'd0:  seg_leds <= 8'b0000_0011;		// 0
				4'd1:  seg_leds <= 8'b1001_1111;		// 1
				4'd2:  seg_leds <= 8'b0010_0101;		// 2
				4'd3:  seg_leds <= 8'b0000_1101;		// 3
				4'd4:  seg_leds <= 8'b1001_1001;		// 4
				4'd5:  seg_leds <= 8'b0100_1001;		// 5
				4'd6:  seg_leds <= 8'b0100_0001;		// 6
				4'd7:  seg_leds <= 8'b0001_1111;		// 7
				4'd8:  seg_leds <= 8'b0000_0001;		// 8
				4'd9:  seg_leds <= 8'b0000_1001;		// 9
				4'd10: seg_leds <= 8'b0001_1001;		// a
				4'd11: seg_leds <= 8'b1100_0001;		// b
				4'd12: seg_leds <= 8'b0110_0011;		// c
				4'd13: seg_leds <= 8'b1000_0101;		// d
				4'd14: seg_leds <= 8'b0110_0001;		// E
				4'd15: seg_leds <= 8'b0111_0001;		// F
				default: seg_leds <= 8'b0000_0011;		// 0
			endcase
		end
	end
	else begin
		seg_nCS  <= 6'b1111_11;
		seg_leds <= 8'hFF;
	end
end

endmodule
